<?php
namespace Home\Controller;

use Think\Controller;

class BookController extends BaseController
{
    /**
     * 产品列表页，默认显示第一个分类
     *
     */
    public function index()
    {
        //产品显示
        $category_list = D("Book")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        $cat_id = I("get.cat_id", 0);
        $_wheresql = "";
        if (!empty($cat_id)) {
            $_wheresql = "cat_id = $cat_id";
        }
        $cat_info = array();
        if (!empty($cat_id)) {
            $cat_info = D("Book")->table("dsk_books_category")->where("id = $cat_id")->find();
        }
        $books_list = D("Book")->table("dsk_books")->where($_wheresql)->order(array('add_time' => "desc"))->select();
        $data = array(
            "category_list" => $category_list,
            "cat_id" => $cat_id,
            "books_list" => $books_list,
            "cat_info" => $cat_info,
            "menu" => "book"
        );
        $this->assign($data);
        $this->display();
    }

    public function info()
    {
        $id = I("get.id", 0);
        $cat_id = I("get.cat_id", 0);
        if (!empty($id)) {
            $book_info = D("Book")->table("dsk_books")->where("id=$id")->find();
            $data = array("book_info" => $book_info);

        }
        $_wheresql = "cat_id = $cat_id";
        $cat_info = array();
        if (!empty($cat_id)) {
            $cat_info = D("Book")->table("dsk_books_category")->where("id = $cat_id")->find();
        }
        $category_list = D("Book")->table("dsk_books_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        $data['category_list'] = $category_list;
        $data['cat_info'] = $cat_info;
        $data['cat_id'] = $cat_id;
        $data['menu'] = "book";
        $this->assign($data);

        $this->display("book_detail");
    }
}